﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Adams.Public.DataAccess.Web.Areas.Membership.Donations
{
    public static class Donate
    {
        public static Adams.Public.DataTransfer.Web.Areas.Membership.Donations.Donate.PageLoad PageLoad()
        {
            var data = new Adams.Public.DataTransfer.Web.Areas.Membership.Donations.Donate.PageLoad();
            
            return data;
        }

        public static int SaveDonation(DataTransfer.Web.Areas.Membership.Donations.Donate.SaveInput input)
        {
            int donationid = DB.RunScalar(@"
IF(@Recurring = 1)
BEGIN
    INSERT INTO Donations.Recurring (
        BucketID
        ,MemberID
        ,Amount
        ,NextProcessOn
        ,StatusID)
    VALUES (
        @BucketID
        ,@MemberID
        ,@Amount
        ,@RecurringStartDate
        ,@StatusID)
    SELECT SCOPE_IDENTITY();
END
ELSE
BEGIN
    INSERT INTO Donations.OneTime (
        BucketID
        ,MemberID
        ,Amount
        ,StatusID)
    VALUES (
        @BucketID
        ,@MemberID
        ,@Amount
        ,@StatusID)
    SELECT SCOPE_IDENTITY();
END
    ", CommandType.Text,
                    DB.CreateParameter("@BucketID", input.BucketID.ToInt()),
                    DB.CreateParameter("@MemberID", input.MemberID),
                    DB.CreateParameter("@Amount", input.Amount),
                    DB.CreateParameter("@StatusID", input.Status),
                    DB.CreateParameter("@Recurring", input.Recurring, SqlDbType.Bit),
                    DB.CreateParameter("@RecurringStartDate", input.RecurringStartDate)).DbInt();

            return donationid;
        }

        public static Adams.Public.DataTransfer.Web.Areas.Membership.Donations.Confirm.PageLoad ConfirmPageLoad(DataTransfer.Web.Areas.Membership.Donations.Confirm.PageLoadInput input)
        {
            var data = new Adams.Public.DataTransfer.Web.Areas.Membership.Donations.Confirm.PageLoad();

            input.Recurring = input.Recurring == "1" ? "true" : "false";

            using (SqlDataReader dr = DB.RunDataReader(@"

IF(@Recurring = 1)
BEGIN
    SELECT B.*
    FROM Donations.Recurring R INNER JOIN Donations.Buckets B ON R.BucketID = B.BucketID
    WHERE R.DonationID = @DonationID AND R.MemberID = @MemberID

    SELECT C.*
    FROM Finance.CreditCards C
    WHERE C.MemberID = @MemberID

    SELECT R.*
    FROM Donations.Recurring R
    WHERE R.DonationID = @DonationID AND R.MemberID = @MemberID AND StatusID = 1
END
ELSE
BEGIN
    SELECT B.*
    FROM Donations.OneTime R INNER JOIN Donations.Buckets B ON R.BucketID = B.BucketID
    WHERE R.DonationID = @DonationID AND R.MemberID = @MemberID

    SELECT C.*
    FROM Finance.CreditCards C
    WHERE C.MemberID = @MemberID

    SELECT R.*
    FROM Donations.OneTime R
    WHERE R.DonationID = @DonationID AND R.MemberID = @MemberID AND StatusID = 1
END
", CommandType.Text,
    DB.CreateParameter("@DonationID", input.DonationID),
    DB.CreateParameter("@MemberID", input.MemberID),
    DB.CreateParameter("@Recurring", input.Recurring.ToBool(), SqlDbType.Bit)))
            {
                data.DonationID = input.DonationID.ToInt();
                data.Recurring = input.Recurring.ToBool();
                dr.Read();
                var bucket = new Shared.Entities.Donations.Bucket() { 
                    ID = dr["BucketID"].DbInt(),
                    Name = dr["Name"].DbString(),
                    IsDefault = dr["IsDefault"].DbBool(),
                    AllowRecurring = dr["AllowRecurring"].DbBool()
                };

                dr.NextResult();

                dr.Read();
                data.CreditCard = new Shared.Entities.Finance.CreditCard() {
                    CardLast4Digits = dr["CardLast4Digits"].DbString()
                };

                dr.NextResult();
                dr.Read();

                if (data.Recurring)
                {
                    data.RecurringDonation = new Shared.Entities.Donations.Recurring() {
                        ID = dr["DonationID"].DbInt(),
                        Amount = dr["Amount"].DbDecimal(),
                        Bucket = bucket
                    };
                }
                else
                {
                    data.OneTimeDonation = new Shared.Entities.Donations.OneTime()
                    {
                        ID = dr["DonationID"].DbInt(),
                        Amount = dr["Amount"].DbDecimal(),
                        Bucket = bucket
                    };
                }
            }

            return data;
        }

        public static void ConfirmDonation(DataTransfer.Web.Areas.Membership.Donations.Confirm.SaveInput input)
        {
            DB.Run(@"
IF(@Recurring = 1)
BEGIN
    UPDATE Donations.Recurring SET StatusID = @StatusID WHERE DonationID = @DonationID AND MemberID = @MemberID AND StatusID = 1
END
ELSE
BEGIN
    UPDATE Donations.OneTime SET StatusID = @StatusID WHERE DonationID = @DonationID AND MemberID = @MemberID AND StatusID = 1
END
    ", CommandType.Text,
                    DB.CreateParameter("@DonationID", input.DonationID.ToInt()),
                    DB.CreateParameter("@MemberID", input.MemberID),
                    DB.CreateParameter("@StatusID", input.Status),
                    DB.CreateParameter("@Recurring", input.Recurring.ToBool(), SqlDbType.Bit));
        }
    }
}